/*load the data*/
cd E:\data\cooperation\democracy
use fdata.dta,clear
sort countrycode year
gen sqrinter=internetuse^2  /*get the sqrt internetuse*/
gen ln_unna_gdppc=log(unna_gdppc)
label var polity "Level of Democracy (Freedom House/Imputed Polity)"
label var internetuse "Internet users (per 10 people)"
label var ln_unna_gdppc "Natural Logarithm of GDP per Capita (Current Prices in US dollar)"
label var wdi_pop65 "Population ages 65 and above (% of total)"
label var wbgi_cce "Control of Corruption"
label var wbgi_gee "Government Effectiveness"
label var wbgi_pse "Political Stability"
label var wbgi_rqe "Regulatory Quality"
label var wbgi_vae "Voice and accountability"
label var wdi_expmilgdp "Military expenditure (% of GDP)"
label var sqrinter "the square of Internet users (per 10 people)"
rename fh_fotpsc press
label var polit_sys "Political System"
save democracy,replace


***************main regression************************
use democracy,clear
xtset countrycode year

/*statistical description for table 1*/
sum polity internetuse  sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_vae wbgi_pse wdi_expmilgdp press polit_sys
corr wbgi_rqe wbgi_pse wbgi_vae wbgi_cce 

/*unit root test*/
xtunitroot fisher polity ,demean lags(aic 2) kernel(bartlett llc) 
xtunitroot llc polity,noconstant demean lags(aic 3) kernel(ba nwest)
/*VIF test*/
lmcol polity ln_unna_gdppc wdi_pop65 wbgi_cce wbgi_vae wbgi_gee wbgi_rqe wbgi_pse wdi_expmilgdp /*table 2*/
lmcol polity  ln_unna_gdppc wdi_pop65 wbgi_cce  wbgi_pse wbgi_vae wdi_expmilgdp
ivreg polity internetuse ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_gee wbgi_rqe wbgi_pse wdi_expmilgdp press
ivvif
ivreg  polity ln_unna_gdppc wdi_pop65 wbgi_cce wbgi_vae wbgi_gee wbgi_rqe wbgi_pse wdi_expmilgdp press
ivvif

/*collinear analysis*/
collin ln_unna_gdppc wdi_pop65 wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp press

/*regresion of the base model*/
xtreg polity internetuse ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store fe1_demo 
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe 
est store fe_demo
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,re 
est store re_demo
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp i.year
est store ols_demo
xtgls L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp i.year ,panels(hetero) /*table S3*/
est store gls_demo
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp i.year,fe
est store two_demo
xtabond polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,lag(1) two
est store gmm_demo
esttab fe1_demo fe_demo re_demo ols_demo gls_demo two_demo, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))  /*table 3*/
   

/*Hausman test*/
hausman  fe_demo re_demo,sigmamore
hausman  fe_demo gls_demo
hausman fe_demo ols_demo
hausman  fe_demo fe1_demo,sigmamore


/*linear quantile mixed model*/
global Rterm_path "C:\Program Files\R\R-3.4.2\bin\i386\Rterm.exe"		
xtreg polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe	
est store q0
xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(0.1)
est store q1
xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(0.25)
est store q2
xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(0.5)
est store q3
xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(0.75)
est store q4
xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(0.9)
est store q5
esttab q0 q1 q2 q3 q4 q5, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))   /*table 5*/

/*linear quantile mixed model with quantiels from 0.01 to 0.99*/
forvalues i = 0.01(0.01)1{
           xtqreg  polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,q(`i')
		   outreg2 using result.csv
          }
seeout /*the result is save in the file of "result.csv"*/

/*group analysis*/
preserve
keep if group==1 /*countries with higher level of democracy */
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store high_demo
esttab  high_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))/*table 4*/
restore


preserve
keep if group==0 /*countries with lower level of democracy */
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store low_demo
esttab  low_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))  /*table 4*/
restore

/*add the ineraction variable*/
gen group_interneruse=group*internetuse

xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store all_demo
xtreg L(0/1)polity internetuse sqrinter group_interneruse ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store all1_demo
esttab  all_demo all1_demo, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC)) /*table 4*/

**********political system*************
preserve
keep if polit_sys==0 /*president*/
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65 wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store high_demo
esttab  high_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))/*table 4*/
restore


preserve
keep if polit_sys==1 /*countries with lower level of democracy */
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store low_demo
esttab  low_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))  /*table 4*/
restore

gen inter=polit_sys*internetuse
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store all_demo
xtreg L(0/1)polity internetuse sqrinter inter group_interneruse ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store all1_demo
esttab  all_demo all1_demo, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC)) /*table 4*/


/*roubust test*/
use testdata,clear
sort countrycode year
gen sqrinter=internetuse^2
gen ln_unna_gdppc=log(unna_gdppc)
xtset  countrycode year
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp wbgi_rqe dr_eg wdi_lifexptot p_durable,fe
est store roubust1_demo
xtreg L(0/1)polity internetuse sqrinter ln_unna_gdppc wdi_pop65  wbgi_cce wbgi_vae wbgi_pse wdi_expmilgdp,fe
est store roubust_demo
esttab roubust_demo roubust1_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(r2 bic, fmt(3 1) label(R-squared AIC))  /*table 6*/








